package cn.house.rent.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;
import java.util.HashMap;
import java.util.Map;

@WebListener
public class RentListener implements HttpSessionAttributeListener {
    private Map<String, HttpSession> sessions = new HashMap<>();

    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        String name = httpSessionBindingEvent.getName();
        if ("username".equals(name)) {//username 是登录账户请求
            String value = (String) httpSessionBindingEvent.getValue();
            //查询集合中是否有当前账户
            if (sessions.containsKey(value)) {
                HttpSession httpSession = sessions.get(value);//获取集合中对应session
                httpSession.invalidate();
            }
            sessions.put(value, httpSessionBindingEvent.getSession());
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        String name = httpSessionBindingEvent.getName();
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
        String name = httpSessionBindingEvent.getName();
    }

}
